package com.demo.serviceI.JobassemblyI;

import com.alibaba.fastjson.JSON;
import com.demo.exception.ServiceException;
import com.demo.mapper.Jobassembly.JobassemblyMapper;
import com.demo.model.Jobassembly.Jobassembly;
import com.demo.model.Jobassembly.JobassemblyQuery.JobassemblyQuery;
import com.demo.service.Jobassembly.JobassemblyService;
import com.demo.tool.paging;
import com.fasterxml.jackson.core.JsonToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class JobassemblyServiceI implements JobassemblyService {


    @Autowired
    private JobassemblyMapper jobassemblyMapper;

    @Override
    public Jobassembly findById(Serializable id) {
        if(id==null){
            throw new ServiceException("id为空,请输入删除id");
        }
        return jobassemblyMapper.findById(id);
    }

    @Override
    public Jobassembly findByName(String name) {
        return null;
    }

    @Override
    public int save(Jobassembly jobassembly) {
        if(jobassembly==null){
            throw new ServiceException("添加对象为空,请检查是否正确提交");
        }
        jobassembly.setStatus(1);
        return jobassemblyMapper.save(jobassembly);
    }

    @Override
    public int saveAll(Collection<Jobassembly> collection) {
        System.out.println("执行了子类的方法");
        for(Jobassembly j:collection){
            if(j.getStatus()==null){
                System.out.println("状态为空");
                j.setStatus(2);
            }
            System.out.println("当前对象"+ JSON.toJSONString(j));
        }
        return jobassemblyMapper.saveAll(collection);
    }

    @Override
    public int update(Jobassembly jobassembly) {
        if(jobassembly==null){
            throw new ServiceException("修改对象为空,请检查是否正确提交");
        }
        return jobassemblyMapper.update(jobassembly);
    }

    @Override
    public int delById(Serializable id) {
        if(id==null){
            throw new ServiceException("删除编号为空,请输入删除编号");
        }
        return jobassemblyMapper.delById(id);
    }

    @Override
    public int delByIdIn(Serializable[] ids) {
        System.out.println(ids+"数组");
        for (Serializable id:ids){
            System.out.println(id+"这是");
        }
        if(ids==null||ids.length<=0){
            throw new ServiceException("删除数组为空,请检查删除数组");
        }
        return jobassemblyMapper.delByIdIn(ids);
    }

    @Override
    public paging findAllpaging(Integer page, Integer pageSize, JobassemblyQuery jobassemblyQuery) throws ParseException {

        //初始化分页对象
        paging<Jobassembly> pagination = new paging(page, pageSize,jobassemblyMapper.countNumber(jobassemblyQuery));
        //为当前页的数据集赋值
        pagination.setData(jobassemblyMapper.findAllpaging(pagination.getOffset(),pagination.getPageSize(),jobassemblyQuery));

        return pagination;
    }

    @Override
    public List<Jobassembly> findAll(JobassemblyQuery jobassemblyQuery) {
        return jobassemblyMapper.findAll(jobassemblyQuery);
    }


    @Override
    public List<Integer> schedulingList() {
        return jobassemblyMapper.schedulingList();
    }
}
